package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishSetmealMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * ClassName: SetmealServiceImpl
 * Package: com.sky.service.impl
 * Description:
 *
 * @Author Dylan_Jun
 * @Create 2024/2/1 13:51
 * @Version 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishSetmealMapper dishSetmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public void save(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.save(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (setmealDishes!=null && setmealDishes.size()>0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            dishSetmealMapper.saveBatch(setmealDishes);
        }
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        //删除两个表中的内容
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        setmealMapper.deleteBatch(ids);
        dishSetmealMapper.deleteBatch(ids);

    }

    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> list = dishSetmealMapper.getByid(id);
        SetmealVO vo = new SetmealVO();
        BeanUtils.copyProperties(setmeal,vo);
        vo.setSetmealDishes(list);
        return vo;
    }

    @Override
    public void update(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        dishSetmealMapper.deleteBatch(Arrays.asList(setmeal.getId()));
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (setmealDishes!=null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> {
                if(dishMapper.getByid(setmealDish.getDishId()).getStatus().equals(StatusConstant.DISABLE)){
                    setmeal.setStatus(StatusConstant.DISABLE);
                }
                setmealDish.setSetmealId(setmeal.getId());
            });
            dishSetmealMapper.saveBatch(setmealDishes);
        }
        setmealMapper.update(setmeal);
    }

    @Override
    public void setStatus(Integer status, Long id) {
        if (status.equals(StatusConstant.ENABLE)) {
            dishSetmealMapper.getByid(id).forEach(setmealDish ->{
                if (dishMapper.getByid(setmealDish.getDishId()).getStatus().equals(StatusConstant.DISABLE)){
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
